package com.example.springboot10_19.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @GetMapping("/index")
    public String index() {

        return "index";
    }

    @GetMapping("/login")
    public String login() {
        return "login";

    }

    @RequestMapping("/login")
    public String toLogin(String name, String password, Model model) {
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);
        Subject currentUser = SecurityUtils.getSubject();
        try {
            currentUser.login(token);
            return "index";
        } catch (UnknownAccountException uae) {
            model.addAttribute("msg", "用户名不存在");
            return "login";
        } catch (IncorrectCredentialsException ice) {
            model.addAttribute("msg", "密码错误");
            return "login";
        }
    }
    @GetMapping("/loginout")
    public String loginout(){
        Subject currentUser= SecurityUtils.getSubject();
        currentUser.logout();
        return "index";
    }
    @GetMapping("/user/normal")
    public  String normal(){
        return "/user/normal";
    }
    @GetMapping("/noauth")
    @ResponseBody()
    public  String noAuth(){
        return  "noauth";
    }
}
